package com.skyline.feed.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.skyline.common.mongo.MongoTemplate;
import com.skyline.feed.dao.PersonalFeedDao;
import com.skyline.feed.type.FeedType;

@Repository("personalFeedDao")
public class PersonalFeedDaoImpl implements PersonalFeedDao {

	@Autowired
	private MongoTemplate mongoTemplate;

	@Value("${feed.personalFeed.collectionsPrefix}")
	private String personalFeedCollectionsPrefix;

	@Value("${feed.personalFeed.maxFeedLevel}")
	private Integer MAX_FEED_LEVEL;

	@Value("${feed.column.time}")
	private String TIME;

	@Value("${feed.column.resourceId}")
	private String RESOURCE_ID;

	@Value("${feed.column.resourceId.Title}")
	private String TITLE;

	@Value("${feed.column.content}")
	private String CONTENT;

	@Value("${feed.column.providerId}")
	private String PROVIDER_ID;

	@Value("${feed.column.providerNickname}")
	private String PROVIDER_NICKNAME;

	@Value("${feed.column.providerPortrait}")
	private String PROVIDER_PORTRAIT;

	@Value("${feed.column.ownerId}")
	private String OWNER_ID;

	@Value("${feed.column.type}")
	private String TYPE;

	@Value("${feed.pagination.pageSize}")
	private Integer PAGE_SIZE;

	@Value("${feed.maxPageSize}")
	private Integer MAX_PAGE_SIZE;

	private List<DBCollection> collections = new ArrayList<DBCollection>();
	
	private static final Log LOGGER = LogFactory.getLog(PersonalFeedDao.class);
	
	private boolean initaled=false;

	private void inital() {
		if (initaled==false||collections.size() == 0) {
			try {
				for (int i = 0; i <= MAX_FEED_LEVEL; i++) {
					DBCollection collection = mongoTemplate.getCollection(personalFeedCollectionsPrefix + i);
					collections.add(i, collection);
				}
				initaled=true;
			} catch (Exception e) {
				LOGGER.error(e);
			}
		}
	}

	@Override
	public void insertFeed(Long resourceId, String title, String content, Long providerId, String providerNickname,
			String providerPortrait, Long ownerId, FeedType type) {
		this.inital();
		BasicDBObject feed = new BasicDBObject();
		feed.put(TIME, (new Date()).getTime());
		feed.put(RESOURCE_ID, resourceId);
		feed.put(TITLE, title);
		feed.put(CONTENT, content);
		feed.put(PROVIDER_ID, providerId);
		feed.put(PROVIDER_NICKNAME, providerNickname);
		feed.put(PROVIDER_PORTRAIT, providerPortrait);
		feed.put(OWNER_ID, ownerId);
		feed.put(TYPE, type.toString());
		collections.get(0).insert(feed);
	}

	@Override
	public DBCursor queryFeeds(Long ownerId, Integer current, Integer level) {
		this.inital();
		// System.out.println(OWNER_ID+":"+ownerId);
		BasicDBObject condition = new BasicDBObject();
		condition.put(OWNER_ID, ownerId);
		BasicDBObject orderBy = new BasicDBObject();
		orderBy.put(TIME, -1);
		DBCursor cursor = collections.get(level).find(condition).skip(current).limit(PAGE_SIZE).sort(orderBy);
		return cursor;
	}

	@Override
	public DBCursor queryFeeds(List<Long> ownerIds, Integer level, Long lastQueryTime) {
		this.inital();
		// 用户Id序列
		Object[] objs = new Object[ownerIds.size()];
		for (int i = 0; i < ownerIds.size(); i++) {
			objs[i] = ownerIds.get(i);
		}
		DBObject ownerIdRequiration = new BasicDBObject("$in", objs);

		// 大于上次查询的时间
		DBObject timeRequiration = new BasicDBObject("$gt", lastQueryTime);

		// 组成复合条件
		DBObject query = new BasicDBObject();

		query.put(TIME, timeRequiration);
		query.put(OWNER_ID, ownerIdRequiration);

		DBCursor cursor = collections.get(level).find(query).limit(MAX_PAGE_SIZE);

		return cursor;
	}
}
